# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgname=openssh
pkgver=8.1p1
pkgrel=1
pkgdesc='Free version of the SSH connectivity tools'
url='https://www.openssh.com/portable.html'
license=('custom:BSD')
arch=('i686' 'x86_64')
groups=('net-utils')
depends=('heimdal' 'libedit' 'libcrypt' 'openssl')
makedepends=('heimdal-devel' 'libedit-devel' 'libcrypt-devel' 'openssl-devel')
source=("https://mirrors.mit.edu/pub/OpenBSD/OpenSSH/portable/${pkgname}-${pkgver}.tar.gz"{,.asc}
        openssh-7.3p1-msys2.patch
        openssh-7.3p1-msys2-setkey.patch
        openssh-7.3p1-msys2-drive-name-in-path.patch)
sha256sums=('02f5dbef3835d0753556f973cd57b4c19b6b1f6cd24c03445e23ac77ca1b93ff'
            'SKIP'
            '9530ab786e343ef576e0154afb850728b4b7101fc1169041da68137dd6ba7f3e'
            '25079cf4a10c1ab70d60302bccaabee513762520dffd7c35285f7aae3ea36087'
            '903b3eee51e492a125cab9c724ad967450307d53e457f025e4432b81cb145af5')
validpgpkeys=('59C2118ED206D927E667EBE3D3E5F56B6D920D30')

backup=('etc/ssh/ssh_config' 'etc/ssh/sshd_config')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"
  patch -p1 -i ${srcdir}/openssh-7.3p1-msys2.patch
  patch -p1 -i ${srcdir}/openssh-7.3p1-msys2-setkey.patch
  patch -p1 -i ${srcdir}/openssh-7.3p1-msys2-drive-name-in-path.patch
  autoreconf -fvi
}

build() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  TEST_SSH_UTF8=no \
  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --sbindir=/usr/bin \
    --libexecdir=/usr/lib/ssh \
    --sysconfdir=/etc/ssh \
    --localstatedir=/var \
    --with-libedit \
    --with-kerberos5=/usr \
    --disable-strip

  make
}

check() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make tests ||
  grep $USER /etc/passwd | grep -q /bin/false
  # connect.sh fails when run with stupid login shell
}

package() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  make DESTDIR="${pkgdir}" install

  #ln -sf ssh.1.gz "${pkgdir}"/usr/share/man/man1/slogin.1.gz
  install -Dm644 LICENCE "${pkgdir}/usr/share/licenses/${pkgname}/LICENCE"

  install -Dm755 contrib/findssl.sh "${pkgdir}"/usr/bin/findssl.sh
  install -Dm755 contrib/ssh-copy-id "${pkgdir}"/usr/bin/ssh-copy-id
  install -Dm644 contrib/ssh-copy-id.1 "${pkgdir}"/usr/share/man/man1/ssh-copy-id.1

  #rm -f "${pkgdir}"/etc/ssh/sshd_config
  #rm -f "${pkgdir}"/usr/bin/sshd.exe
  #rm -f "${pkgdir}"/usr/sbin/sshd.exe
  #rm -f "${pkgdir}"/usr/lib/ssh/sftp-server.exe
}
